package com.youmingxing.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;
import com.youmingxing.R;
import com.youmingxing.utils.SharePreferenceUtil;
import com.youmingxing.view.DialogManager;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageCenter;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageTable;
import com.youmingxing.view.clipheadphoto.clip1.msg.MessageWithInt;

import java.util.List;


/**
 * JHS
 */

public class BaseActivity extends FragmentActivity {

    FrameLayout mTitleContainer;
    TextView mTitleTextView;
    View mTitleView;

    public FragmentManager mFragmentManager;

    protected TextView rightTv;
    protected ImageView leftImg, rightImage;

    private FrameLayout layout;
    private RelativeLayout errorLayout;
    protected SharePreferenceUtil sharePreferenceUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        sharePreferenceUtil = new SharePreferenceUtil(this);
        setContentView(R.layout.base_activity);
        mTitleContainer = (FrameLayout) findViewById(R.id.header_title);
        mTitleTextView = (TextView) findViewById(R.id.header_tv_title);
        leftImg = (ImageView) findViewById(R.id.header_iv_left);
        rightTv = (TextView) findViewById(R.id.header_tv_right);
        rightImage = (ImageView) findViewById(R.id.header_iv_right);
        mFragmentManager = getSupportFragmentManager();
        layout = (FrameLayout) findViewById(R.id.llcontent);
        errorLayout = (RelativeLayout) findViewById(R.id.error);
        errorLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog();
                onErrorClick();
            }
        });
    }

    public void BaseSetContentView(int layoutID) {
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(layoutID, null);
        layout.addView(view);
        errorLayout.setVisibility(View.GONE);
    }

    public void showErrorPage(boolean isShow) {
        if (isShow) {
            layout.setVisibility(View.GONE);
            errorLayout.setVisibility(View.VISIBLE);
        } else {
            layout.setVisibility(View.VISIBLE);
            errorLayout.setVisibility(View.GONE);
        }
    }

    public void onErrorClick() {

    }

    /**
     * 设置标题名称
     *
     * @param titleName 标题名称
     */
    public void setTopbarName(String titleName) {
        if (mTitleView != null) {
            mTitleContainer.removeView(mTitleView);
            mTitleView = null;
            mTitleTextView.setVisibility(View.VISIBLE);
        }
        mTitleTextView.setText(titleName);
    }

    @Override
    protected void onStop() {
        super.onStop();
        MessageWithInt msgToSend = new MessageWithInt();
        msgToSend.setMsgId(MessageTable.iSRunning);
        if (!isAppOnForeground()) {
            msgToSend.setInt(2);
        } else {
            msgToSend.setInt(0);
        }
        MessageCenter.getInstance().sendMessage(msgToSend);
    }


    public boolean isAppOnForeground() {
        // Returns a list of application processes that are running on the
        // device

        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = getApplicationContext().getPackageName();

        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null)
            return false;

        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }

        return false;
    }

    /**
     * 设置标题名称
     *
     * @param titleResId 标题资源id
     */
    public void setTopbarName(int titleResId) {
        if (mTitleView != null) {
            mTitleContainer.removeView(mTitleView);
            mTitleView = null;
            mTitleTextView.setVisibility(View.VISIBLE);
        }
        mTitleTextView.setText(titleResId);
    }

    public void setTopbarView(View titleView) {
        if (mTitleView == titleView) {
            return;
        }
        if (mTitleView != null) {
            mTitleContainer.removeView(mTitleView);
            mTitleView = null;
        }
        mTitleView = titleView;
        mTitleContainer.addView(mTitleView);
        mTitleTextView.setVisibility(View.GONE);
    }

    /**
     * 右边为文字按钮
     *
     * @param isLeftShow   左侧按钮是否显示
     * @param isRightShow  右侧按钮是否显示
     * @param leftOnclick  左侧按钮点击事件 如果不需要特殊处理传null  finish事件已经实现
     * @param rightOnclick 左侧按钮点击事件
     */
    public void setLeftAndBtn(boolean isLeftShow, boolean isRightShow, View.OnClickListener leftOnclick, View.OnClickListener rightOnclick) {
        if (isLeftShow) {
            leftImg.setVisibility(View.VISIBLE);
            if (leftOnclick != null) {
                leftImg.setOnClickListener(leftOnclick);
            }
        } else {
            leftImg.setVisibility(View.INVISIBLE);
        }
        if (isRightShow) {
            rightTv.setVisibility(View.VISIBLE);
            if (rightOnclick != null) {
                rightTv.setOnClickListener(rightOnclick);
            }
        } else {
            rightTv.setVisibility(View.INVISIBLE);
        }
    }

    /**
     * 左右图片按钮
     *
     * @param isLeftShow        左侧按钮是否显示
     * @param isRightShow       右侧按钮是否显示
     * @param isrightImage      右侧图片按钮是否显示
     * @param rightText         右侧按钮文字设置
     * @param leftOnclick       左侧按钮点击事件
     * @param rightOnclick      右侧按钮点击事件
     * @param rightImageOnclick 右侧图片按钮点击事件
     */
    public void setLeftAndrightTv(boolean isLeftShow, boolean isRightShow, boolean isrightImage, int rightText, View.OnClickListener leftOnclick, View.OnClickListener rightOnclick, View.OnClickListener rightImageOnclick) {
        if (isLeftShow) {
            leftImg.setVisibility(View.VISIBLE);
            if (leftOnclick != null) {
                leftImg.setOnClickListener(leftOnclick);
            }
        } else {
            leftImg.setVisibility(View.INVISIBLE);
        }
        if (isRightShow) {
            rightTv.setVisibility(View.VISIBLE);
            if (rightText != -1) {
                rightTv.setText(getString(rightText));
            }
            if (rightOnclick != null) {
                rightTv.setOnClickListener(rightOnclick);
            }
        } else {
            rightTv.setVisibility(View.GONE);
        }
        if (isrightImage) {
            rightImage.setVisibility(View.VISIBLE);
            if (rightImageOnclick != null) {
                rightImage.setOnClickListener(rightImageOnclick);
            }
        } else {
            rightImage.setVisibility(View.GONE);
        }
    }

    public void setrightTvIsEnable(boolean isEnable) {
        if (isEnable) {
            rightTv.setTextColor(getResources().getColor(R.color.whiteff));
            rightTv.setEnabled(true);
        } else {
            rightTv.setTextColor(getResources().getColor(R.color.whitecc));
            rightTv.setEnabled(false);
        }
    }


    public void showToast(int StringID) {
        Toast.makeText(getApplication(), StringID, Toast.LENGTH_SHORT).show();
    }

    public void showToast(String toastStr) {
        Toast.makeText(getApplication(), toastStr, Toast.LENGTH_SHORT).show();
    }

    public void showDialog(String msg) {
        DialogManager.showDialog(this, msg);
    }

    public void showDialog() {
        DialogManager.showDialog(this);
    }


    public void hideDialog() {
        DialogManager.dismissDialog();
    }


    /**
     * 默认左侧按钮关闭
     */
    public void leftCloseOnclick(View v) {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void finish() {
        super.finish();
        if (getIntent().getExtras() != null) {
            if (getIntent().getExtras().getBoolean("start_main")) {
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideDialog();
    }
}
